package com.yan.common.util;

import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;

/**
 * 雪花算法工具类，基于Twitter的Snowflake算法实现
 * 封装 hutool 雪花算法工具类
 * @author yanchen
 * @since 2025/2/15 13:00
 */
public class SnowflakeUtil {
    private static volatile Snowflake SNOWFLAKE;

    private SnowflakeUtil() {
        // 私有构造函数，防止实例化
    }

    public static Snowflake getInstance() {
        if (SNOWFLAKE == null) {
            synchronized (SnowflakeUtil.class) {
                if (SNOWFLAKE == null) {
                    SNOWFLAKE = IdUtil.getSnowflake();
                }
            }
        }
        return SNOWFLAKE;
    }

    public static Long getId() {
        return getInstance().nextId();
    }

    public static String getIdStr() {
        return getInstance().nextIdStr();
    }

    /**
     * 将字符串转换为Long类型
     * @param idStr id字符串
     * @return id Long类型
     */
    public static Long idStrToId(String idStr) {
        return Long.valueOf(idStr);
    }
}
